package com.powernode.ex;

import com.powernode.constant.BusinessEnum;
import com.powernode.model.Result;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @description:
 * @author: mky
 * @date: 2024/7/9 9:37
 * @version: 1.0
 */
@RestControllerAdvice
public class GlobalExceptionHandle {

    @ExceptionHandler(RuntimeException.class)
    public Result<String> holeRuntimeException(RuntimeException e){
        return Result.failure(BusinessEnum.SERVER_INNER_ERROR);
    }

    /**
     * 处理：权限不足异常
     * @param e
     * @throws AccessDeniedException
     */
    @ExceptionHandler(AccessDeniedException.class)
    public void accessDeniedException(AccessDeniedException e) throws AccessDeniedException {
        // 抛出AccessDeniedException异常，走自定义AccessDeniedException异常处理
        throw e;
    }

    @ExceptionHandler()
    public Result<String> holeBusinessException(BusinessException e){
        return Result.failure(BusinessEnum.SERVER_INNER_ERROR.getCode(),e.getMessage());
    }
}
